#!/bin/sh

revision="ImageMod v0.1.8 (2023-06-19 MIBonk & MIB-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
fi
echo -ne "\n$ME-$thisname---->\n" >> $LOG

if [ -f $TMP/backup.mib ] || [ -f $TMP/reboot.mib ] || [ -f $TMP/flash.mib ]; then
	echo -ne "Backup, flash or reboot are already running...\nWill stop here!"
	return 2> /dev/null
fi

MIBCAP=1
. $thisdir/../config/MIBCHECK

# script variables
IMGDIR="/net/mmx/mnt/app/eso/hmi/lsd/Resources"

case $1 in

-m) {

trap '' 2

if [[ "$TRAINVERSION" = *SKG13_?452* ]]; then
	UnitVersion="SKG13_x452x"
	echo -e "\nInstall image MOD on "$UnitVersion"\n"
	mount -uw /net/mmx/mnt/app
	if [ ! -f $IMGDIR/skin0/images.bck ];then
		cp -f $IMGDIR/skin0/images.mcf $IMGDIR/skin0/images.bck 2>&1 | $TEE -a $LOG
		echo -e "Create backup for skin0..." 2>&1 | $TEE -a $LOG
	else
		echo -e "Image backup for skin0 already exists!\n\n" 2>&1 | $TEE -a $LOG
	fi
	# create patched /skin0/images.mcf from /skin0/images.mcf
	echo -ne "Patching images files part 1 - skin0\n--- Duration: ~1 minute\n" | $TEE -a $LOG
	$RDIFF -f patch $IMGDIR/skin0/images.bck $VOLUME/mod/images/$UnitVersion/skin0/images.mcf.delta $IMGDIR/skin0/images.patch 2>> $LOG

#todo: move chksum to filename like the ambient files!
	CHECKSUM_Skin0="38c66e034bd1f5b61b74d3d97b0058eee87f65f5"

	checksum_unit="$($SHA1 $IMGDIR/skin0/images.patch | awk '{print $1}')" 2>> $LOG
	if [ $CHECKSUM_Skin0 = $checksum_unit ]; then
		mv -f $IMGDIR/skin0/images.patch $IMGDIR/skin0/images.mcf 2>> $LOG
		chmod +x $IMGDIR/skin0/images.mcf 2>> $LOG
		echo "--> DONE\n" | $TEE -a $LOG
	else
		echo "Something went wrong\nRevert to images.mcf from backup\nCancel install\n" | $TEE -a $LOG
		rm -f $IMGDIR/skin0/images.bck 2>> $LOG
		rm -f $IMGDIR/skin0/images.patch 2>> $LOG
		exit 1
	fi
	ls -l $IMGDIR/skin0/ >> $LOG
	# copy files from skin1 to skin3
	echo -ne "Built up skin3\n" | $TEE -a $LOG
	cp -crf $IMGDIR/skin1/Alphabets/* $IMGDIR/skin3/Alphabets/ 2>> $LOG
	cp -crf $IMGDIR/skin1/Fonts/* $IMGDIR/skin3/Fonts/ 2>> $LOG
	cp -crf $IMGDIR/skin1/i18n/* $IMGDIR/skin3/i18n/ 2>> $LOG
	cp -crf $IMGDIR/skin1/view/* $IMGDIR/skin3/view/ 2>> $LOG
	cp -crf $IMGDIR/skin1/ambienceColorMap.res $IMGDIR/skin3/ 2>> $LOG
	cp -crf $IMGDIR/skin1/images.res $IMGDIR/skin3/ 2>> $LOG
	cp -crf $IMGDIR/skin1/viewhandler.zip $IMGDIR/skin3/ 2>> $LOG
	cp -crf $IMGDIR/skin1/images.mcf $IMGDIR/skin3/ 2>> $LOG
	ls -l $IMGDIR/skin3/ >> $LOG
	# create patched /skin3/images.mcf from /skin1/images.mcf
	echo -ne "Patching images files part 2 - skin3\n--- Duration: ~1 minute\n" | $TEE -a $LOG
		$RDIFF -f patch $IMGDIR/skin3/images.mcf $VOLUME/mod/images/$UnitVersion/skin3/images.mcf.delta $IMGDIR/skin3/images.patch 2>> $LOG

#todo: move chksum to filename like the ambient files!
	CHECKSUM_skin3="0a322d581cb320c60bc6be87e2817fcdf20aa2f2"

	checksum_unit="$($SHA1 $IMGDIR/skin3/images.patch | awk '{print $1}')" 2>> $LOG
	if [ $CHECKSUM_skin3 = $checksum_unit ]; then
		cp -crf $VOLUME/mod/images/$UnitVersion/skin3/info.txt $IMGDIR/skin3/ 2>> $LOG
		mv -f $IMGDIR/skin3/images.patch $IMGDIR/skin3/images.mcf 2>> $LOG
		chmod +x $IMGDIR/skin3/images.mcf 2>> $LOG
		echo "--> DONE\n" | $TEE -a $LOG
	else
		echo "Something went wrong\nRevert unit to stock\nCancel install\n" | $TEE -a $LOG
		rm -rf $IMGDIR/skin3 2>> $LOG
		exit 1
	fi
	echo -e "MOD Installed...\n" 2>&1 | $TEE -a $LOG
	mount -ur /net/mmx/mnt/app
	echo -e "\n ---> image mod done, be happy ;-)\n" 2>&1 | $TEE -a $LOG
	. $thisdir/reboot -t 10
# ----------------------------------------------------------------------------------------------------------------
elif [[ "$TRAINVERSION" = *VWG13_?452* ]]; then
	UnitVersion="VWG13_x452x"
	echo -e "\nInstall image MOD on "$UnitVersion"\n"
	mount -uw /net/mmx/mnt/app
	# copy files from skin1 to skin5
	echo -ne "--- Built up skin5\n" | $TEE -a $LOG
	cp -crf $IMGDIR/skin1/Alphabets/* $IMGDIR/skin5/Alphabets/ 2>> $LOG
	cp -crf $IMGDIR/skin1/Fonts/* $IMGDIR/skin5/Fonts/ 2>> $LOG
	cp -crf $IMGDIR/skin1/i18n/* $IMGDIR/skin5/i18n/ 2>> $LOG
	cp -crf $IMGDIR/skin1/view/* $IMGDIR/skin5/view/ 2>> $LOG
	cp -crf $IMGDIR/skin1/images.res $IMGDIR/skin5/ 2>> $LOG
	cp -crf $IMGDIR/skin1/viewhandler.zip $IMGDIR/skin5/ 2>> $LOG
	cp -crf $IMGDIR/skin1/images.mcf $IMGDIR/skin5/ 2>> $LOG
	ls -l $IMGDIR/skin5/ >> $LOG
	# create patched /skin5/images.mcf from /skin1/images.mcf
	echo -ne "Patching images file - skin5\n--- Duration: ~1 minute\n" | $TEE -a $LOG
	$RDIFF -f patch $IMGDIR/skin5/images.mcf $VOLUME/mod/images/$UnitVersion/skin5/images.mcf.delta $IMGDIR/skin5/images.patch 2>> $LOG

#todo: move chksum to filename like the ambient files!
	CHECKSUM_skin5="f02b40962936c7e77aa362b8fa671ac3a0237387"

	checksum_unit="$($SHA1 $IMGDIR/skin5/images.patch | awk '{print $1}')" 2>> $LOG
	if [ $CHECKSUM_skin5 = $checksum_unit ]; then
		cp -crf $VOLUME/mod/images/$UnitVersion/skin5/info.txt $IMGDIR/skin5/ 2>> $LOG
		cp -crf $VOLUME/mod/images/$UnitVersion/skin5/ambienceColorMap.res $IMGDIR/skin5/ 2>> $LOG
		mv -f $IMGDIR/skin5/images.patch $IMGDIR/skin5/images.mcf 2>> $LOG
		chmod +x $IMGDIR/skin5/images.mcf 2>> $LOG
		echo "--> DONE\n" | $TEE -a $LOG
	else
		echo "Something went wrong\nRevert unit to stock\nCancel install\n" | $TEE -a $LOG
		rm -rf $IMGDIR/skin5 2>> $LOG
		exit 1
	fi
	# copy files from skin5 to skin6
	echo -ne "--- Built up skin6\n" | $TEE -a $LOG
	cp -crf $IMGDIR/skin5/* $IMGDIR/skin6/ 2>> $LOG
	cp -crf $VOLUME/mod/images/$UnitVersion/skin6/info.txt $IMGDIR/skin6/ 2>> $LOG
	cp -crf $VOLUME/mod/images/$UnitVersion/skin6/ambienceColorMap.res $IMGDIR/skin6/ 2>> $LOG
	ls -l $IMGDIR/skin6/ >> $LOG
	echo -e "MOD Installed...\n" 2>&1 | $TEE -a $LOG
	mount -ur /net/mmx/mnt/app
	echo -e "\n ---> image mod done, be happy ;-)\n" 2>&1 | $TEE -a $LOG
	. $thisdir/reboot -t 10
else
	echo -ne "no supported train found - will stop here\n" | $TEE -a $LOG
fi

trap 2

return 2> /dev/null

};;

-b) {

trap '' 2

echo -ne "Remove image MOD\n"
mount -uw /net/mmx/mnt/app
if [[ "$TRAINVERSION" = *$SKG13_P4526* ]]; then
	if [ -f $IMGDIR/skin0/images.bck ];then
		mv -f $IMGDIR/skin0/images.bck $IMGDIR/skin0/images.mcf 2>&1 | $TEE -a $LOG
	else
		echo -e "Image backup did not exist!" 2>&1 | $TEE -a $LOG
	fi
	rm -rf $IMGDIR/skin3 2>> $LOG
elif [[ "$TRAINVERSION" = *$VWG13_P4521* ]]; then
	rm -rf $IMGDIR/skin5 2>> $LOG
	rm -rf $IMGDIR/skin6 2>> $LOG
fi
echo -e "do restore..." 2>&1 | $TEE -a $LOG
echo -e "MOD removed...\n" 2>&1 | $TEE -a $LOG
. $thisdir/reboot -t 10

trap 2

return 2> /dev/null

};;

# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname
		echo ""
		echo "        -m 		install MOD images"
		echo "        -b 		remove MOD, back to original"
		echo "        --help		show this help"
		echo ""		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
		
	};;
	
	esac

exit 0

